package com.ruoyi.report_ljlm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_ljlm.domain.YsdgsfbgBsDetail;
import com.ruoyi.report_ljlm.domain.vo.YsdgsfbgBsDetailVo;
import com.ruoyi.report_ljlm.mapper.YsdgsfbgBsDetailMapper;
import com.ruoyi.syrw.annotation.ComputeReportResult;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_ljlm.domain.bo.YsdgsfbgBsInfoBo;
import com.ruoyi.report_ljlm.domain.vo.YsdgsfbgBsInfoVo;
import com.ruoyi.report_ljlm.domain.YsdgsfbgBsInfo;
import com.ruoyi.report_ljlm.mapper.YsdgsfbgBsInfoMapper;
import com.ruoyi.report_ljlm.service.IYsdgsfbgBsInfoService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 压实度试验检测报告(灌砂法)Service业务层处理
 *
 * @author llb
 * @date 2024-08-27
 */
@RequiredArgsConstructor
@Service
public class YsdgsfbgBsInfoServiceImpl implements IYsdgsfbgBsInfoService {

    private final YsdgsfbgBsInfoMapper baseMapper;
    private final YsdgsfbgBsDetailMapper detailMapper;

    /**
     * 查询压实度试验检测报告(灌砂法)
     */
    @Override
    @FIllBasicInfo
    public YsdgsfbgBsInfoVo queryById(Long excelId) {
        YsdgsfbgBsInfoVo ysdgsfbgBsInfoVo = baseMapper.selectVoById(excelId);
        List<YsdgsfbgBsDetailVo> detailList = detailMapper.selectVoList(
            new LambdaQueryWrapper<YsdgsfbgBsDetail>().eq(YsdgsfbgBsDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(detailList)) {
            ysdgsfbgBsInfoVo.setDetailList(detailList);
        }
        return ysdgsfbgBsInfoVo;
    }

    /**
     * 查询压实度试验检测报告(灌砂法)列表
     */
    @Override
    public TableDataInfo<YsdgsfbgBsInfoVo> queryPageList(YsdgsfbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<YsdgsfbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<YsdgsfbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询压实度试验检测报告(灌砂法)列表
     */
    @Override
    public List<YsdgsfbgBsInfoVo> queryList(YsdgsfbgBsInfoBo bo) {
        LambdaQueryWrapper<YsdgsfbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<YsdgsfbgBsInfo> buildQueryWrapper(YsdgsfbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<YsdgsfbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, YsdgsfbgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, YsdgsfbgBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), YsdgsfbgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), YsdgsfbgBsInfo::getBaogbh, bo.getBaogbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), YsdgsfbgBsInfo::getShigwtdw, bo.getShigwtdw());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), YsdgsfbgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), YsdgsfbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), YsdgsfbgBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), YsdgsfbgBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), YsdgsfbgBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), YsdgsfbgBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), YsdgsfbgBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), YsdgsfbgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), YsdgsfbgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), YsdgsfbgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());

        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), YsdgsfbgBsInfo::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), YsdgsfbgBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), YsdgsfbgBsInfo::getBgFlag, bo.getBgFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), YsdgsfbgBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), YsdgsfbgBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), YsdgsfbgBsInfo::getFuh, bo.getFuh());
        lqw.eq(StringUtils.isNotBlank(bo.getRiq()), YsdgsfbgBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), YsdgsfbgBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), YsdgsfbgBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), YsdgsfbgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        return lqw;
    }

    /**
     * 新增压实度试验检测报告(灌砂法)
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(YsdgsfbgBsInfoBo bo) {
        YsdgsfbgBsInfo add = BeanUtil.toBean(bo, YsdgsfbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            ArrayList<YsdgsfbgBsDetail> details = new ArrayList<>(32);
            for (int i = 1; i <= 32; i++) {
                details.add(new YsdgsfbgBsDetail(bo.getExcelId()));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改压实度试验检测报告(灌砂法)
     */
    @Override
    @DSTransactional
    @ComputeReportResult
    public Boolean updateByBo(YsdgsfbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        YsdgsfbgBsInfo update = BeanUtil.toBean(bo, YsdgsfbgBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getDetailList(), YsdgsfbgBsDetail.class));
        }
        return flag;
    }

    private void fillExcelResult(YsdgsfbgBsInfoBo bo) {
        if (ObjUtil.isNull(bo.getDaibz()) || ObjUtil.isNull(bo.getYasdsjz())) {
            bo.setExcelResult(0);
            return;
        }
        if (!BigDecimalUtil.isAnyOneEmpty(new BigDecimal(bo.getDaibz()), new BigDecimal(bo.getYasdsjz()))) {
            if (BigDecimalUtil.isLessOrEqual(new BigDecimal(bo.getDaibz()), new BigDecimal(bo.getYasdsjz()))) {
                bo.setExcelResult(2);
                return;
            }
        }
        bo.setExcelResult(1);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(YsdgsfbgBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除压实度试验检测报告(灌砂法)
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public YsdgsfbgBsInfoVo jcjl(YsdgsfbgBsInfoVo vo) {
        StringBuilder result = new StringBuilder("检测结论：经检测，所检指标符合《公路工程质量检验评定标准第一册土建工程》JTG F80/1-2017及设计文件要求。");
        vo.setJiancjl(result.toString());
        return vo;
    }
}
